package com.cdp.zwy.zwy_manager_back_v1.exception;


import com.cdp.zwy.zwy_manager_back_v1.common.ServiceException;
import com.cdp.zwy.zwy_manager_back_v1.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author zwy
 * @version 1.0
 * @description: GlobalExceptionHandler
 * @date 2025/4/6 22:10
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<Result> handleServiceException(ServiceException ex) {
        // 记录日志
        log.error("业务异常: 错误码={}, 错误信息={}", ex.getErrorCode().getCode(), ex.getErrorCode().getMsg(), ex);

        // 返回统一错误响应
        Result response = new Result(
                ex.getErrorCode().getCode(),
                ex.getErrorCode().getMsg(),
                null
        );
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }
}
